package script.esx;

import cn.com.betasoft.dsat.probe.groovy.SimException;
/**
原生变量：g_col_pars g_col_other g_rets
原生变量：g_res(g_shc, g_dbcon)
样例
[btmon@localhost btmon]$ UNIX95= ps -e -o 'state,user,group,pid,ppid,vsz,tty,etime,time,pcpu,start,pmem,rsz,args'
S USER     GROUP   PID  PPID   VSZ TT           ELAPSED     TIME %CPU  STARTED %MEM RSZ COMMAND
S root     root      1     0  1528 ?         1-20:26:41 00:00:34  0.0   Sep 24  0.0 172 init
S root     root      2     1     0 ?         1-20:26:41 00:00:00  0.0   Sep 24  0.0 0 [keventd]
*/
def json = [];

def cmd = "export LANG=C;UNIX95= ps -e -o 'state,user,group,pid,ppid,vsz,tty,etime,time,pcpu,start,pmem,rsz,args'";
if (g_shc.type == 'ssh2'){
	cmd = "export LANG=C;UNIX95= ps -e -o 'state,user,group,pid,ppid,vsz,tty,etime,time,pcpu,start,pmem,rsz,args'";
}
def String ret = g_shc.collect(cmd);
if(ret.contains("not found")){
	throw new SimException(8010,"ESX.OSProcess:没有找到命令.");
}
if(ret.contains("Permission denied")){
	throw new SimException(8011,"ESX.OSProcess:没有权限执行命令.");
}

def matched = false;
ret.split("\n").each{
	matched = true;
	def matcher = (it =~ /(\S+)\s+(\S+)\s+(\S+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\d+\.\d+)\s+(.*)\s+(\d+\.\d+)\s+(\d+)\s+(.*)/);
	if (matcher.matches()){
		def process = ['Status':matcher[0][1], 
		              'UID':matcher[0][2], 
		              'UserGroup':matcher[0][3],
		              'PID':matcher[0][4].toInteger(), 
		              'PPID':matcher[0][5].toInteger(), 
		              'SZ':matcher[0][6].toInteger(), 
		              'TTY':matcher[0][7], 
		              'ElapsedTime':matcher[0][8], 
		              'CPUTime':matcher[0][9], 
		              'CPU':matcher[0][10], 
		              'BeginTime':matcher[0][11].trim(),
		              'MEMRatio':matcher[0][12],
		              'MEM':matcher[0][13].toLong(),  
		              'CMD':matcher[0][14]
			      	];
		json.add(process);
	}
};
if(!matched){
	throw new SimException(8012,"ESX.OSProcess:返回数据不匹配.");
}
g_rets = json;